package com.authine.cloudpivot.ext.domain.query;

import com.authine.cloudpivot.ext.domain.vo.VendorGoodsVO;
import com.authine.cloudpivot.ext.dto.ConvertTypeEnum;
import com.authine.cloudpivot.ext.dto.goods.VendorGoodsEntity;
import com.authine.cloudpivot.ext.utils.BigDecimalUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品保存Query
 *
 * @author 张磊
 * @date 2019年6月4日11:43:45
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GoodsInputQuery {
    private Integer profileId;
    private String brand;
    private String name;
    private String model;
    private String spec;
    private String barCode;
    private String goldenTax;
    private String unspsc;
    private Integer detailId;
    private String place;
    private String deliveryPlace;
    private String images;
    private String description;
    private String technical;
    private String packaging;
    private String memo;
    private String goodsId;
    private String catalog;
    private String unit;
    private String packageUnit;
    private Double packageNum;
    private Double pretaxPrice;
    private Double price;
    private Double taxRate;
    private String sku;
    private Double stock;
    private String warranty;
    private Integer delivery;
    private Double minOrder;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date expiry;
    private Double length;
    private Double width;
    private Double height;
    private Double weight;
    private Integer freightTemplateId;
    private Boolean saled;
    private Boolean enabled;
    private String deliveryClaim;
    private String orgFullname;
    private String clientId;
    private String clientGoodsId;
    private Integer safeStock;
    private Integer stockDelivery;
    private Boolean core;

    public static List<GoodsInputQuery> toGoodsInputQueryList(List<VendorGoodsVO> list, ConvertTypeEnum typeEnum) {
        List<GoodsInputQuery> goodsList = new ArrayList<>();

        list.forEach(g -> {
            GoodsInputQuery goodsInputQuery = new GoodsInputQuery();
            goodsInputQuery.setBrand(g.getBrand());
            goodsInputQuery.setName(g.getName());
            goodsInputQuery.setModel(g.getModel());
            goodsInputQuery.setMemo(g.getMemo());
            goodsInputQuery.setSpec(g.getSpec());
            goodsInputQuery.setCatalog(g.getCategory());
            goodsInputQuery.setUnit(g.getUnit());
            goodsInputQuery.setPackageNum(g.getPackageNum());
            goodsInputQuery.setPackageUnit(g.getPackageUnit());
            goodsInputQuery.setMinOrder(g.getMinOrder());
            goodsInputQuery.setTaxRate(g.getTaxRate());

            if ((g.getPretaxPrice() != null && g.getPrice() != null) || g.getPrice() == null) {
                goodsInputQuery.setPretaxPrice(g.getPretaxPrice());
                goodsInputQuery.setPrice(BigDecimalUtils.round(BigDecimalUtils.mul(new BigDecimal(g.getPretaxPrice()), new BigDecimal(g.getTaxRate() + 1)), 4).doubleValue());
            } else if (g.getPretaxPrice() == null) {
                goodsInputQuery.setPrice(g.getPrice());
                goodsInputQuery.setPretaxPrice(BigDecimalUtils.round(BigDecimalUtils.div(new BigDecimal(g.getPrice()), new BigDecimal(1 + g.getTaxRate())), 4).doubleValue());
            }
            goodsInputQuery.setStock(g.getStock());
            goodsInputQuery.setPlace(g.getPlace());
            goodsInputQuery.setDeliveryPlace(g.getDeliveryPlace());
            goodsInputQuery.setDelivery(g.getDelivery() == null ? 0 : g.getDelivery());
            goodsInputQuery.setWarranty(g.getWarranty());
            goodsInputQuery.setSku(g.getSku());
            goodsInputQuery.setBarCode(g.getBarCode());
            goodsInputQuery.setGoldenTax(g.getGoldenTax());
            goodsInputQuery.setUnspsc(g.getUnspsc());
            goodsInputQuery.setDescription(g.getDescription());
            goodsInputQuery.setTechnical(g.getTechnical());
            goodsInputQuery.setPackaging(g.getPackaging());
            goodsInputQuery.setExpiry(g.getExpiry());
            goodsInputQuery.setLength(g.getLength());
            goodsInputQuery.setWidth(g.getWidth());
            goodsInputQuery.setHeight(g.getHeight());
            goodsInputQuery.setMemo(g.getMemo());
            goodsInputQuery.setGoodsId(g.getVendorGoodsId());
            goodsInputQuery.setEnabled(g.getEnabled());
            goodsInputQuery.setDeliveryClaim(g.getDeliveryClaim());
            goodsInputQuery.setClientId(g.getClientId());
            String clientGoodsId = g.getClientGoodsId();
            goodsInputQuery.setClientGoodsId(StringUtils.isBlank(clientGoodsId) ? null : clientGoodsId.trim());
            //todo  images不能为空 ,并且需要特殊处理, 先下载处理成 云枢图片数据类型
//            goodsInputQuery.setImages(StringUtils.isBlank(g.getImages()) ? "" : g.getImages());

            if (ConvertTypeEnum.PM.equals(typeEnum)) {
                goodsInputQuery.setOrgFullname(g.getOrgFullname());
                goodsInputQuery.setCore(g.getCore());
            }
            goodsInputQuery.setStockDelivery(g.getStockDelivery());
            goodsInputQuery.setSafeStock(g.getSafeStock());
            goodsList.add(goodsInputQuery);
        });
        return goodsList;
    }

    public static VendorGoodsEntity toVendorGoodsEntity(GoodsInputQuery giq) {

//        SerializeConfig config = new SerializeConfig();
//        config.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
//        String giqStr = JSON.toJSONString(giq,config);
//        VendorGoodsEntity entity = JSON.parseObject(giqStr, VendorGoodsEntity.class);
//        entity.setId(null==giq.getGoodsId()?null:String.valueOf(giq.getGoodsId()));
//        return entity;


        return VendorGoodsEntity.builder()
                .id(giq.getGoodsId() == null ? null : String.valueOf(giq.getGoodsId()))
                .brand(giq.getBrand())
                .name(giq.getName())
                .model(giq.getModel())
                .spec(giq.getSpec())
                .bar_code(giq.getBarCode())
                .golden_tax(giq.getGoldenTax())
                .unspsc(giq.getUnspsc())
                .place(giq.getPlace())
                .delivery_place(giq.getDeliveryPlace())
//                        .images()
                .description(giq.getDescription())
                .technical(giq.getTechnical())
                .packaging(giq.getPackaging())
                .memo(giq.getMemo())
                .catalog(giq.getCatalog())
                .unit(giq.getUnit())
                .package_unit(giq.getPackageUnit())
                .package_num(doubleToBigDecimal(giq.getPackageNum()))
                .pretax_price(doubleToBigDecimal(giq.getPretaxPrice()))
                .price(doubleToBigDecimal(giq.getPrice()))
                .tax_rate(doubleToBigDecimal(giq.getTaxRate()))
                .sku(giq.getSku())
                .stock(doubleToBigDecimal(giq.getStock()))
                .warranty(giq.getWarranty())
                .delivery(null == giq.getDelivery() ? null : String.valueOf(giq.getDelivery()))
                .min_order(doubleToBigDecimal(giq.getMinOrder()))
                .expiry(giq.getExpiry())
                .length(doubleToBigDecimal(giq.getLength()))
                .width(doubleToBigDecimal(giq.getWidth()))
                .height(doubleToBigDecimal(giq.getHeight()))
                .weight(doubleToBigDecimal(giq.getWeight()))
                .freight_template_id(null == giq.getFreightTemplateId() ? null : String.valueOf(giq.getFreightTemplateId()))
                .saled(giq.getSaled())
                .enabled(giq.getEnabled())
                .delivery_claim(giq.getDeliveryClaim())
                .org_fullname(giq.getOrgFullname())
                .client_id(giq.getClientId())
                .client_goods_id(giq.getClientGoodsId())
                .safe_stock(null == giq.getSafeStock() ? null : String.valueOf(giq.getSafeStock()))
                .stock_delivery(null == giq.getStockDelivery() ? null : String.valueOf(giq.getStockDelivery()))
                .core(giq.getCore())
                .build();
    }

    public static List<VendorGoodsEntity> toVendorGoodsEntity(List<GoodsInputQuery> list) {
        return list.stream()
                .map(GoodsInputQuery::toVendorGoodsEntity)
                .collect(Collectors.toList());

    }

    private static BigDecimal doubleToBigDecimal(Double d) {
        return d == null ? null : BigDecimal.valueOf(d);
    }
}
